﻿/**
 * =========================================
 * 基于jQuery封装组件 (抽象、复用、共性、工具)
 * @author jjl<jiangjialiang@teemax.com.cn>
 * @date 2016/9/27
 * =========================================
 */
(function(window, $) {

	var MinSu = window.MinSu || {};
	
	/**
	 * -----------------------------------------
	 * 民宿查询条件
	 * @author jjl<jiangjialiang@teemax.com.cn>
	 * @remark 
	 * -----------------------------------------
	 */
	var MSearch = function(){};
	$.extend(MSearch.prototype, {
		/**
		 * 参数列表
		 * @param {String} param 参数列表
		 * @param {String} key 替换的key
		 * @param {String} value 替换的value
		 * @return {String} paramStr 返回重新组装的参数列表
		 */
		msearch_parameters: function(param, key, value){
			
			var checkinDate = param.split("-")[0];
			var leaveDate = param.split("-")[1];
			var hotelName = param.split("-")[2];
			var areaId = param.split("-")[3];
			var areaHotId = param.split("-")[4];
			var roomType = param.split("-")[5];
			var priceRange = param.split("-")[6];
			var themeId = param.split("-")[7];
			var paging = param.split("-")[8];
			
			// 地区
			if(key == 3 || key.toLowerCase() == "areaid"){
				areaId = value;
			}
			
			// 地标
			if(key == 4 || key.toLowerCase() == "areahotid"){
				areaHotId = value;
			}
			
			// 房间
			if(key == 5 || key.toLowerCase() == "roomtype"){
				roomType = value;
			}
			
			// 价格
			if(key == 6 || key.toLowerCase() == "pricerange"){
				priceRange = value;
			}
			
			// 主题
			if(key == 7 || key.toLowerCase() == "themeid"){
				themeId = value;
			}
			
			// 分页栏
			if(key == 8 || key.toLowerCase() == "paging"){
				paging = value;
			}
			
			var paramStr = checkinDate+"-"+
						   leaveDate+"-"+
						   hotelName+"-"+
						   areaId+"-"+
						   areaHotId+"-"+
						   roomType+"-"+
						   priceRange+"-"+
						   themeId+"-"+
						   paging;
			 
			 return paramStr;
		},
		/**
		 * 截取url地址参数列表
		 * @param {String} url 地址
		 * @return {String} param 参数列表
		 */
		msearch_urlParam: function(url){
			var param = url.split('?')[1];
			var checkinDate = "";	// 开始时间  [0]
			var leaveDate = "";		// 结束时间  [1]
			var hotelName = "";		// 民宿名称  [2]
			var areaId = "";		// 地区id  [3]
			var areaHotId = "";		// 地标id  [4]
			var roomType = "";		// 房间类型  [5]
			var priceRange = "";	// 价格区间  [6]
			var themeId = "";		// 主题id  [7]
			var paging = "";		// 分页            [8]
			// 直接点击"特色活动" / 直接点击"地图搜索"
			
			if(typeof(param) == 'undefined'){
				// 不为空
				// !"is not null" = false
				// !!"is not null" = true
				// !! = 双重否定
				if(!!sessionStorage.checkinDate){
					checkinDate = sessionStorage.checkinDate.replace(/-/g, ",");
				// 为空
				}else{
					$('#checkinDate').val(MUtil.prototype.mutil_timeFormat(new Date(), "yyyy-MM-dd"));
					checkinDate = $('#checkinDate').val().replace(/-/g, ",");
				}
				
				if(!!sessionStorage.leaveDate){
					leaveDate = sessionStorage.leaveDate.replace(/-/g, ",");
				}else{
					$('#leaveDate').val(MUtil.prototype.mutil_timeFormat(new Date((new Date()/1000+86400)*1000), "yyyy-MM-dd"));
					leaveDate = $('#leaveDate').val().replace(/-/g, ",");
				}
				
				hotelName = $('#keySearchVal').val();
				if(hotelName == "" || typeof(hotelName) == "undefined"){
					hotelName = "0";
				}
				areaId = "0";
				areaHotId = "0";
				roomType = "0";
				priceRange = "0,0";
				themeId = "0";
				paging = "1";
			// 首页跳转"特色活动" / 地图搜索-查询
			}else{
				if(!!sessionStorage.checkinDate){
					checkinDate = sessionStorage.checkinDate.replace(/-/g, ",");
				}else{
					checkinDate = param.split("-")[0];
				}
				
				if(!!sessionStorage.leaveDate){
					leaveDate = sessionStorage.leaveDate.replace(/-/g, ",");
				}else{
					leaveDate = param.split("-")[1];
				}
				
				hotelName = $('#keySearchVal').val();
				if(hotelName == "" || typeof(hotelName) == "undefined"){
					hotelName = param.split("-")[2];
				}
				areaId = param.split("-")[3];
				areaHotId = param.split("-")[4];
				roomType = param.split("-")[5];
				priceRange = param.split("-")[6];
				themeId = param.split("-")[7];
				paging = param.split("-")[8];
			}
			param = checkinDate+"-"+
					leaveDate+"-"+
					hotelName+"-"+
					areaId+"-"+
					areaHotId+"-"+
					roomType+"-"+
					priceRange+"-"+
					themeId+"-"+
					paging;
			return param;
		},
		//当前日期，日期+1;
		getcurDate: function(url){
			var param = url.split('?')[1];
			var checkinDate = "";	// 开始时间  [0]
			var leaveDate = "";		// 结束时间  [1]
			if(typeof(param) == 'string'){
				if(!!sessionStorage.checkinDate){
					checkinDate = sessionStorage.checkinDate.replace(/-/g, ",");
				// 为空
				}else{
					$('#checkinDate').val(MUtil.prototype.mutil_timeFormat(new Date(), "yyyy-MM-dd"));
					checkinDate = $('#checkinDate').val().replace(/-/g, ",");
				}
				
				if(!!sessionStorage.leaveDate){
					leaveDate = sessionStorage.leaveDate.replace(/-/g, ",");
				}else{
					$('#leaveDate').val(MUtil.prototype.mutil_timeFormat(new Date((new Date()/1000+86400)*1000), "yyyy-MM-dd"));
					leaveDate = $('#leaveDate').val().replace(/-/g, ",");
				}
			}
			param = checkinDate+"-"+
					leaveDate+"-";
			return param;
		},
		/**
		 * ajax请求参数
		 * @param {String} param 参数列表
		 * @return {Object} data 请求参数对象
		 */
		msearch_data: function(param){
			var checkinDate = param.split("-")[0].replace(/,/g, "-");
			var leaveDate = param.split("-")[1].replace(/,/g, "-");
			$('#checkinDate').val(checkinDate);
			$('#leaveDate').val(leaveDate);
			var hotelName = param.split("-")[2];
			var areaId = param.split("-")[3];
			var areaHotId = param.split("-")[4];
			var roomType = param.split("-")[5];
			var priceRange = param.split("-")[6];
			var themeId = param.split("-")[7];
			var paging = param.split("-")[8];
			var hasRoom = "N";
			var data = {};
			// 选择不同地区,重置地标id
			if(sessionStorage.areaId != areaId){
				areaHotId = "0";
			}
			sessionStorage.areaId = areaId;
			if(areaId != "0" && areaId != "" && typeof(areaId) != "undefined"){
				data["hotelSearchPo.areaId"] = areaId;
			}else{
				areaHotId = "0";	// 地区不限,重置地标
			}
			if(areaHotId != "0" && areaHotId != "" && typeof(areaHotId) != "undefined"){
				data["hotelSearchPo.hotAreaId"] = areaHotId;
			}
			if(hotelName != "0" && hotelName != "" && typeof(hotelName) != "undefined"){
				data["hotelSearchPo.name"] = hotelName;
			}
			if(roomType != "0" && roomType != "" && typeof(roomType) != "undefined"){
				data["hotelSearchPo.personNum"] = roomType;
				hasRoom = "Y";
			}
			if(priceRange != "0,0" && priceRange != "" && typeof(priceRange) != "undefined"){
/*				var minPrice = priceRange.split(",")[0];
				var maxPrice = priceRange.split(",")[1];
				data["hotelSearchPo.minPrice"] = minPrice;
				data["hotelSearchPo.maxPrice"] = maxPrice;*/
				data["hotelSearchPo.priceStr"] = priceRange.replace(',', '!');
			}
			if(themeId != "0" && themeId != "" && typeof(themeId) != "undefined"){
				data["hotelSearchPo.tags"] = themeId;
			}
			data["hotelSearchPo.hasRoom"] = hasRoom;
			data["hotelSearchPo.fromDay"] = checkinDate;
			data["hotelSearchPo.toDay"] = leaveDate;
			data["hotelSearchPo.countryId"] = "001";  	// 000台湾  001中国
			data["hotelSearchPo.cityId"] = "001190";			// 001193宁波
			data["hotelSearchPo.page"] = paging;
			data["hotelSearchPo.order"] = "priority";
			data["hotelSearchPo.sort"] = "desc";
			return data;
		},
		/**
		 * 查询条件-地区
		 * @param {String} param 参数列表
		 */
		msearch_area: function(param){
			var areaId = param.split("-")[3];
			if($('#checkBody .checkList:eq(0)').html()==''){
				$.ajax({  
					url: '/area!queryAreaNameList.ajax',
					data: {'parentAreaId': "001190"},
					dataType: "json",  
					success: function(data) {
						$('#hotel_search_list').css('position', 'inherit');
						$('.checkBody .load').remove();
						var areaLiStr = "";
						if(typeof(data.dataList) != "undefined"){
							var paramStr = MSearch.prototype.msearch_parameters(param, 'areaId', '0');
							if(areaId == "0" || areaId == "" || typeof(areaId) == "undefined"){
								areaLiStr += '<a href="?'+paramStr+'" class="active"><span>不限</span></a>';
							}else{
								areaLiStr += '<a href="?'+paramStr+'"><span>不限</span></a>';
							}
							for(var i=0; i<data.dataList.length; i++){
								if(data.dataList[i].areaName != '宁波'){
									var paramStr = MSearch.prototype.msearch_parameters(param, 'areaId', data.dataList[i].areaId);
									if(areaId == data.dataList[i].areaId){
										areaLiStr += '<a href="?'+paramStr+'" class="active"><span>'+data.dataList[i].areaName+'</span></a>';
									}else{
										areaLiStr += '<a href="?'+paramStr+'"><span>'+data.dataList[i].areaName+'</span></a>';
									}
								}
							}
						}
						$('#checkBody .checkList:eq(0)').html(areaLiStr);
					}  
				}); 
			}
		},
		/**
		 * 查询条件-地标
		 * @param {String} param 参数列表
		 */
		msearch_areaHot: function(param){
			var areaId = param.split("-")[3];
			// 选择地区<其它>,截取id值
			if(areaId.indexOf(',') > 0){
				var length = areaId.split(",").length;
				areaId = areaId.split(",")[length-1];
			}
			
			// 地区不限,隐藏地标,否则显示地标
			if(areaId == "0"){
				$('#checkBody ul:eq(1)').hide();
			}else{
				$('#checkBody ul:eq(1)').show();
			}
			var areaHotId = param.split("-")[4];
			$.ajax({  
				url: '/areaHot!getAreaHotByAreaId.ajax',
				data: {'areaHotVo.areaId': areaId},
				dataType: "json",  
				success: function(data) {
					if(data.code == "success"){
						var areaHotLiStr = "";
						if(!!data.data){
							var ahid = data.data.areaHotId;
							if($('#checkBody .checkList:eq(1)').html()==''){
								$.ajax({  
									url: '/areaHot!queryAreaHotNameList.ajax',
									data: {'areaHotVo.areaHotId': ahid},
									dataType: "json",  
									success: function(data) {
										if(typeof(data.dataList) != "undefined"){
											// 拼接地标id
											var areaHotIdStr = "";
											for(var i=0; i<data.dataList.length; i++){
												if(!!data.dataList[i].areaHotId){
													if(data.dataList.length == 1){
														areaHotIdStr += data.dataList[i].areaHotId;
													}else{
														areaHotIdStr += data.dataList[i].areaHotId + ",";
														if(i == data.dataList.length-1){
															areaHotIdStr += data.dataList[i].areaHotId;
														}
													}
												}
											}
											
											var paramStr = MSearch.prototype.msearch_parameters(param, 'areaHotId', '0');
											if(areaHotId == "0" || areaHotId == "" || typeof(areaHotId) == "undefined" || (areaHotIdStr.indexOf(areaHotId) < 0 && areaHotId != "0")){
												areaHotLiStr += '<a id="areaHot" href="?'+paramStr+'" class="active"><span>不限</span></a>';
											}else{
												areaHotLiStr += '<a id="areaHot" href="?'+paramStr+'"><span>不限</span></a>';
											}
											for(var j=0; j<data.dataList.length; j++){
												var paramStr = MSearch.prototype.msearch_parameters(param, 'areaHotId', data.dataList[j].areaHotId);
												if(areaHotId == data.dataList[j].areaHotId){
													areaHotLiStr += '<a href="?'+paramStr+'" class="active"><span>'+data.dataList[j].name+'</span></a>';
												}else{
													areaHotLiStr += '<a href="?'+paramStr+'"><span>'+data.dataList[j].name+'</span></a>';
												}
											}
										}
										$('#checkBody .checkList:eq(1)').html(areaHotLiStr);
									}  
								}); 
							}
						}
					}
				}  
			});
		},
		/**
		 * 查询条件-房间
		 * @param {String} param 参数列表
		 */
		msearch_roomType: function(param){
			var roomType = param.split("-")[5];
			if($('#checkBody .checkList:eq(2)').html()==''){
				var roomTypeArr = ['不限','单人','双人间','三人间','四人间','多人间'];
				var roomTypeLiStr = "";
				var index = 0;
				var paramStr = MSearch.prototype.msearch_parameters(param, 'roomType', index);
				if(Number(roomType) == index || roomType == "" || typeof(roomType) == "undefined"){
					roomTypeLiStr += '<a href="?'+paramStr+'" class="active"><span>'+roomTypeArr[index]+'</span></a>';
				}else{
					roomTypeLiStr += '<a href="?'+paramStr+'"><span>'+roomTypeArr[index]+'</span></a>';
				}
				for(var i=1; i<roomTypeArr.length; i++){
					var paramStr = MSearch.prototype.msearch_parameters(param, 'roomType', i);
					if(roomType == i){
						roomTypeLiStr += '<a href="?'+paramStr+'" class="active"><span>'+roomTypeArr[i]+'</span></a>';
					}else{
						roomTypeLiStr += '<a href="?'+paramStr+'"><span>'+roomTypeArr[i]+'</span></a>';
					}
				}
				$('#checkBody .checkList:eq(2)').html(roomTypeLiStr);
			}
		},
		/**
		 * 查询条件-价格
		 * @param {String} param 参数列表
		 */
		msearch_priceRange: function(param){
			var priceRange = param.split("-")[6];
			if($('#checkBody .checkList:eq(3)').html()==''){
				var priceRangeKeyArr = ['0,0','0,300','300,500','500,800','800,1200','1200,'];
				var priceRangeValArr = ['不限','300元以下','300-500元','500-800元','800-1200元','1200元以上'];
				var priceRangeLiStr = "";
				var index = "0,0";
				var paramStr = MSearch.prototype.msearch_parameters(param, 'priceRange', index);
				if(priceRange == index || priceRange == "" || typeof(priceRange) == "undefined"){
					priceRangeLiStr += '<a href="?'+paramStr+'" class="active"><span>'+priceRangeValArr[0]+'</span></a>';
				}else{
					priceRangeLiStr += '<a href="?'+paramStr+'"><span>'+priceRangeValArr[0]+'</span></a>';
				}
				for(var i=1; i<priceRangeValArr.length; i++){
					var paramStr = MSearch.prototype.msearch_parameters(param, 'priceRange', priceRangeKeyArr[i]);
					if(priceRange == priceRangeKeyArr[i]){
						priceRangeLiStr += 
							'<a href="?'+paramStr+'" class="active"><span>'+priceRangeValArr[i]+'</span></a>';
					}else{
						priceRangeLiStr += 
							'<a href="?'+paramStr+'"><span>'+priceRangeValArr[i]+'</span></a>';
					}
				}
				$('#checkBody .checkList:eq(3)').html(priceRangeLiStr);
			}
		},
		/**
		 * 查询条件-主题
		 * @param {String} param 参数列表
		 */
		msearch_theme: function(param){
			var themeId = param.split("-")[7];
			if($('#checkBody .checkList:eq(4)').html()==''){
				$.ajax({  
					url: '/configTagType!queryVoList.ajax',
					dataType: "json",  
					success: function(data) {
						var themeLiStr = "";
						if(typeof(data.data[0]) != "undefined"){
							var paramStr = MSearch.prototype.msearch_parameters(param, 'themeId', '0');
							if(themeId == "0" || themeId == "" || typeof(themeId) == "undefined"){
								themeLiStr += '<a href="?'+paramStr+'" class="active"><span>不限</span></a>';
							}else{
								themeLiStr += '<a href="?'+paramStr+'"><span>不限</span></a>';
							}
							for(var i=0; i<data.data[0].configTagVoList.length; i++){
								var tagId = data.data[0].configTagVoList[i].tagId;
								var paramStr = MSearch.prototype.msearch_parameters(param, 'themeId', tagId);
								if(themeId == tagId){
									themeLiStr += '<a href="?'+paramStr+'" class="active"><span>'+data.data[0].configTagVoList[i].name+'</span></a>';
								}else{
									themeLiStr += '<a href="?'+paramStr+'"><span>'+data.data[0].configTagVoList[i].name+'</span></a>';
								}
							}
						}
						$('#checkBody .checkList:eq(4)').html(themeLiStr);
					}  
				}); 
			}
		}
	});
	
	/**
	 * -----------------------------------------
	 * 分页
	 * @author jjl<jiangjialiang@teemax.com.cn>
	 * @remark 
	 * -----------------------------------------
	 */
	var MPage = function(){};
	$.extend(MPage.prototype, {
		/**
		 * 分页栏-参数拼接
		 * @param {String} param 参数列表
		 * @param {String} key 替换的key
		 * @param {String} value 替换的value
		 * @return {String} 返回重新组装的参数列表
		 */
		mpage_paramJoint: function(param, key, value){
			if(param != ""){
				return MSearch.prototype.msearch_parameters(param, key, value);
			}else{
				return value;
			}
		},
		/**
		 * 分页栏
		 * @param {Object} pageObj 分页对象
		 * @param {String} param 参数列表
		 * @param {int} pageNo 自定义分页显示条目数
		 * @retrun {String} page html
		 */
		mpage_pageBar: function(pageObj, param, pageNo){
			var i;
			var pagebar = "";
			if(pageObj.total != 0){
				var lastpage = pageObj.lastpage;			// 最后一页
				var nextpage = pageObj.nextpage;			// 下一页
				var page = pageObj.page;					// 第几页
				var pagesize = pageObj.pagesize;			// 每页显示条目数
				var pagetotal = pageObj.pagetotal;			// 总页数
				var previouspage = pageObj.previouspage;	// 上一页
				var total = pageObj.total;					// 总条目数
				
				// 总页数 <= pageNo
				if(pagetotal <= pageNo){
					for(i=1; i<=pagetotal; i++){
						var	paramStr = MPage.prototype.mpage_paramJoint(param, 'paging', i);
						if(i == page){
							pagebar += '<a href="?'+paramStr+'" class="current">'+i+'</a>';
						}else{
							pagebar += '<a href="?'+paramStr+'">'+i+'</a>';
						}
					}
				}
				
				// 总页数 > pageNo
				if(pagetotal > pageNo){
					/*
					 * 注：每一页分页栏显示pageNo条分页数
					 * 初始页分页栏：01 02 03 04 05 ...
					 * 下一页分页栏：11 12 13 14 15 ...
					 */
					// 初始分页栏, 无上一页分页栏
					if(parseInt((page-1)/pageNo) == 0){
						for(i=1; i<=pageNo; i++){
							var paramStr = MPage.prototype.mpage_paramJoint(param, 'paging', i);
							if(i == page){
								pagebar += '<a href="?'+paramStr+'" class="current">'+i+'</a>';
							}else{
								pagebar += '<a href="?'+paramStr+'">'+i+'</a>';
							}
						}
						var nextParam = MPage.prototype.mpage_paramJoint(param, 'paging', i);
						pagebar += '<a href="?'+nextParam+'" class="next">〉</a>';
					// 无下一页分页栏
					}else if(parseInt((page-1)/pageNo) == parseInt(pagetotal/pageNo)){
						var prevParam = MPage.prototype.mpage_paramJoint(param, 'paging', (parseInt((page-1)/pageNo)*pageNo));
						pagebar += '<a href="?'+prevParam+'" class="page-no preve" style="cursor:default;">〈</a>';
						for(i=parseInt(pagetotal/pageNo)*pageNo+1; i<=pagetotal; i++){
							var paramStr = MPage.prototype.mpage_paramJoint(param, 'paging', i);
							if(i == page){
								pagebar += '<a href="?'+paramStr+'" class="current">'+i+'</a>';
							}else{
								pagebar += '<a href="?'+paramStr+'">'+i+'</a>';
							}
						}
					// 既有上一页分页栏,又有下一页分页栏
					}else{
						var prevParam = MPage.prototype.mpage_paramJoint(param, 'paging', (parseInt((page-1)/pageNo)*pageNo));
						pagebar += '<a href="?'+prevParam+'" class="page-no preve" style="cursor:default;">〈</a>';
						for(i=parseInt((page-1)/pageNo)*pageNo+1; i<=parseInt((page-1)/pageNo)*pageNo+pageNo; i++){
							var paramStr = MSearch.prototype.msearch_parameters(param, 'paging', i);
							if(i == page){
								pagebar += '<a href="?'+paramStr+'" class="current">'+i+'</a>';
							}else{
								pagebar += '<a href="?'+paramStr+'">'+i+'</a>';
							}
						}
						var nextParam = MPage.prototype.mpage_paramJoint(param, 'paging', i);
						pagebar += '<a href="?'+nextParam+'" class="next">〉</a>';
					}
				}
			}
			return pagebar;
		}
	});
	
	/**
	 * -----------------------------------------
	 * 工具
	 * @author jjl<jiangjialiang@teemax.com.cn>
	 * @remark 
	 * -----------------------------------------
	 */
	var MUtil = function(){};
	$.extend(MUtil.prototype, {
		/**
		 * 对Date的扩展，将 Date 转化为指定格式的String
		 * 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
		 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
		 * (new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2016-07-02 08:09:04.423
		 * (new Date()).Format("yyyy-M-d H:m:s.S")      ==> 2016-7-2 8:9:4.18
		 * @param time 时间
		 * @param format 格式
		 * @return format 格式化过的日期格式
		 */
		mutil_timeFormat: function(time, format){
		    var o = {
		        "M+": time.getMonth()+1, 					// 月份 
		        "d+": time.getDate(), 						// 日 
		        "H+": time.getHours(), 						// 小时 
		        "m+": time.getMinutes(), 					// 分 
		        "s+": time.getSeconds(), 					// 秒 
		        "q+": Math.floor((time.getMonth()+3)/3),	// 季度 
		        "S": time.getMilliseconds() 				// 毫秒 
		    };
		    if(/(y+)/.test(format)) format = format.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
		    for(var k in o)
		    if(new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		    return format;
		},
		/**
	     * 时间戳转日期时间 || 日期格式化
		 * y/Y=年 M=月 d/D=日 h/H=时 m=分 s/S=秒 w/W=星期 z/Z=周几 (注: y/yyy/yyyy=2017 yy=17)
		 * 注: 只有 m/M 区分大小写，如包含汉字，则格式为: yyyy年MM月dd日 hh时mm分ss秒
		 *     yyyyMMdd = yyyyMd = YYYyMD = 2017/02/18
		 *     yMdhms = smhdMy = yyyy/MM/dd hh:mm:ss = 2017/02/18 23:50:15
		 *     hh:mm = hhmm = hm = 23:50
		 *     y-M-d = 2017-02-18
		 *     yyyy-MM-dd hh:mm:ss z = 2017-02-18 23:50:15 (周六)
		 *     字母:大小写和顺序先后都支持(除 m/M 字母外)
		 *     format参数为空,返回默认格式-英文
		 *     format参数为其它字母(除yMdhmswz外) 或 特殊字符,则也返回默认格式-英文
		 *     format参数为纯汉字,返回默认格式-中文
		 *     format参数为其它字母+汉字,返回默认格式-中文
		 *     总之, 只要参数的值在包含的匹配项中就会进行比对
	     * @param {int/String/Object} timestamp 时间戳 || 日期对象
	     * @param {String} format 转换格式指定(空返回默认格式)
	     * @returns {String} date_time_joint 根据格式返回值
	     */
	    mutil_timestampConvertDate: function(timestamp, format){
	    	var date = '';
	    	if(!!timestamp){
				if(typeof(timestamp) == 'string' && !Number(timestamp)){
					return 'Value should not be a string';
				}else if(typeof(timestamp) == 'number'|| !!Number(timestamp)){
					date = new Date(parseInt(timestamp));
				}else if(typeof(timestamp) == 'object' && timestamp instanceof Date){
					date = timestamp;
				}else{
					return 'Not the date object';
				}
	    	}else{
				return 'Is a null or undefined value';
			}
	    	
	        var year = date.getFullYear();		// 年  
	        var month = date.getMonth()+1;  	// 月
	        var day = date.getDate();  			// 日
	        var hour = date.getHours();  		// 时
	        var mins = date.getMinutes();  		// 分
	        var second = date.getSeconds();  	// 秒
	        var week = date.getDay();			// 周几
	        var arr = new Array("日", "一", "二", "三", "四", "五", "六");

	    	if(month < 10){
	    		month = '0'+month;
	    	}
	    	if(day < 10){
	    		day = '0'+day;
	    	}
	    	if(hour < 10){
	    		hour = '0'+hour;
	    	}
	    	if(mins < 10){
	    		mins = '0'+mins;
	    	}
	    	if(second < 10){
	    		second = '0'+second;
	    	}
	    	
	    	var letter = '';			// 字母
	    	var repetition = '';		// 重复
	    	var date_symbol = '/';		// 日期符号
			var time_symbol = ':';		// 时间符号
	    	var date_time_joint = '';	// 日期时间拼接
			var default_en = 
				year+'/'+month+'/'+day+' '+hour+':'+mins+':'+second;			// 默认格式-英文
			var default_ch = 
				year+'年'+month+'月'+day+'日 '+hour+'时'+mins+'分'+second+'秒';	// 默认格式-中文
			
    		format = format.replace(/\s/g, '');							// 去空格
    		letter = format.replace(/[a-zA-Z]/g, '');	    			// 去字母
    		repetition = format.replace(/(.)(?=.*\1)/g, '');			// 去重复
			var rep = repetition.replace(/[mM]/g, '').toLowerCase();	// 去除字母m/M, 再转小写
    		var symbol = letter.replace(/(.)(?=.*\1)/g, '');			// 取符号
			var reg = /[^\u0000-\u00FF]/g;								// 汉字
			var flag = reg.test(format);
			var regex = new RegExp('y', 'g');							// 使用g表示整个字符串都要匹配
			var result = format.toLowerCase().match(regex);
			var count = !result ? 0 : result.length;

			if(symbol.length > 1){
				date_symbol = symbol.substring(0, symbol.length-1);
				time_symbol = symbol.substring(1);
			}else{
				date_symbol = !!symbol ? symbol : date_symbol;
				time_symbol = !!symbol ? symbol : time_symbol;
			}

    		if(rep.indexOf('y') >= 0){
				if(flag){
					date_time_joint += count == 2 ? String(year).substr(2)+'年' : year+'年';
				}else{
					date_time_joint += count == 2 ? String(year).substr(2) : year;
				}
	    	}
	    	if(repetition.indexOf('M') >= 0){
				if(flag){
					date_time_joint += month+'月';
				}else{
					date_time_joint += rep.indexOf('y') >= 0 ? (date_symbol+month) : month;
				}	
	    	}
	    	if(rep.indexOf('d') >= 0){
				if(flag){
					date_time_joint += (day+'日');
				}else{
					date_time_joint += (date_symbol+day);
				}
				if(rep.indexOf('h') >= 0 || repetition.indexOf('m') >= 0 || rep.indexOf('s') >= 0){
					date_time_joint += ' ';
				}
	    	}
				
    		if(rep.indexOf('h') >= 0){
				if(flag){
					date_time_joint += hour+'时';
				}else{
					date_time_joint += hour;
				}
	    	}
	    	if(repetition.indexOf('m') >= 0){
				if(flag){
					date_time_joint += mins+'分';
				}else{
	    			date_time_joint += rep.indexOf('h') >= 0 ? (time_symbol+mins) : mins;
				}
	    	}
	    	if(rep.indexOf('s') >= 0 ){
				if(flag){
					date_time_joint += (second+'秒');
				}else{
	    			date_time_joint += (time_symbol+second);
				}
	    	}

			if(rep.indexOf('w') >= 0){
	    		date_time_joint += ' (星期'+arr[week]+') ';
	    	}
			if(rep.indexOf('z') >= 0){
	    		date_time_joint += ' (周'+arr[week]+') ';
	    	}
			
			if(!date_time_joint && flag){
				date_time_joint = default_ch;		
			}

			if(!date_time_joint){
				date_time_joint = default_en;		
			}
	    	
	    	return date_time_joint;
	    },
	    /**
		 * 指定位置插入字符
		 * @param {String} str 原字符串
		 * @param {String} flg 要插入的字符串
		 * @param {int} sn 要插入的位置
		 * @return {String} newStr 新字符串
		 */
		mutil_insertFlg: function(str, flg, sn){
			var newStr = "";
			for(var i=0; i<str.length; i+=sn){
				var tmp = str.substring(i, i+sn);
				if(i == 0){
					newStr += tmp + flg;
				}else{
					newStr += tmp;
				}
			}
			return newStr;
		},
		/**
		 * 数字转换中文(非金额)
		 * @param {String} num 数值字符串
		 */
		mutil_digital2Chinese: function(num){
			// 数值类型
			if(typeof(num) == "number"){
				num = String(num);	// Number to String
			// 对象类型
			}else if(typeof(num) == "object"){
				swal('参数为对象类型，请传入正确的数值类型！');
				return false;
			// 是否为数字
			}else if(isNaN(num)){
				swal('不是一个数字！');
				return false;
			}
		    var china = new Array('零','一','二','三','四','五','六','七','八','九');
		    var arr = new Array();
		    var english = num.split("");
		    for(var i=0; i<english.length; i++){
		        arr[i] = china[english[i]];
		    }
		    return arr.join("");
		},
		/**
		 * 取得目标iframe中src所包含的参数
		 * @param {String} iframeId 目标iframe的id
		 * @return Object 参数名值对,{参数名: 参数值,……}
		 */
		mutil_getIframeParams: function(iframeId){
			var regexpParam = /\??([\w\d%]+)=([\s\w\d\u4e00-\u9fa5%]*)&?/g; // 分离参数的正则表达式
			// 取得iframe的url
		    var url = $(window.parent.document).find("#"+iframeId).attr("src");
		    var paramMap = null; 
		    if(!!url){
		        var ret;
		        paramMap = {};	// 初始化结果集
		        // 开始循环查找url中的参数,并以键值对形式放入结果集
		        while((ret = regexpParam.exec(url)) != null) {
		            // ret[1]是参数名,ret[2]是参数值
		            paramMap[ret[1]] = ret[2];
		        }
		    }
		    return paramMap;
		},
		/**
		 * 设置<li>标签的值
		 * @param {String} ul id名 || class名
		 * @param {Array} values li内容
		 */
		mutil_setILiValue: function(ul, values){
		    $(ul).find("li").each(function(i, n){
				$(this).html(values[i]); // 为当前li赋值
			});
		},
		/**
		 * 追加图片 div html
		 * @param {String} div id名 || class名
		 * @param {String} find children div
		 * @param {String} eq 匹配一个给定索引值的元素
		 * @param {String} name 名称
		 * @param {String} urlPath 图片路径
		 * @param {int} index 标识 
		 */
		mutil_appendPicsDivHtml: function(div, find, eq, name, urlPath, index){
			var picsDivHtml = 
				'<a href="javascript:void(0);" data-index="'+index+'" class="gallery-thumb " pic-type="6">'+
					'<img bigpic="'+urlPath+'" alt="'+name+'" src="'+urlPath+'">'+
					'<span class="pic_name">'+name+'</span>'+
				'</a>';
			$(div).find(find).eq(eq).append(picsDivHtml);
		}
	});
	
	/**
	 * -----------------------------------------
	 * 用户
	 * @author jjl<jiangjialiang@teemax.com.cn>
	 * @remark 
	 * -----------------------------------------
	 */
	var MUser = function(){};
	$.extend(MUser.prototype, {
		/**
		 * 获取用户信息
		 * @param {Object} data 请求参数
		 * @param {String} differentiate 区分
		 */
		muser_queryUser: function(data, differentiate){
			$.ajax({
				type: "post",
				url: "/user!queryUserVo.ajax",
				data: data,
				dataType: "json",
				success: function(resultMap){					
					var name='';
					var nick = resultMap.nick;
					if(!nick){
						nick = '';
						name=resultMap.account;
					}else{
						name=nick;
					}
					var icon = resultMap.icon;
					if(icon == null || icon == 'null' || typeof(icon) == 'undefined'){
						icon = '/images/center/user_tx.jpg';
					}else{
						icon = icon;
					}
					var account = resultMap.account;
					var sex = resultMap.sex;
					if(!sex){
						sex = '';
					}
					var createDate = new Date(resultMap.createDate);
					var address = resultMap.address;
					if(!address){
						address = '';
					}
					var realName = resultMap.realName;
					if(!realName){
						realName = '';
					}
					var mobile = resultMap.mobile;
					if(!mobile){
						mobile = '';
					}
					var email = resultMap.email;
					if(!email){
						email = '';
					}
					
					var index='',order='',lineorder='',coupon='',wish='',mesg='',profile='',className='active';
					switch(differentiate){
						case 'index':	// 账户信息
							index = className;
							$('.userphoto img').attr("src", icon);
							if($('.usermesg').html() == ''){
								var li = 
									'<li>'+
										'<p class="name">' + nick + '</p>'+ 
										'<p class="time">'+createDate.getFullYear()+"-"+(createDate.getMonth()+1)+"-"+createDate.getDate()+'加入</p>'+ 
										'<p>性别：<span>'+sex+'</span></p>'+
										'<p>居住地：<span>'+address+'</span></p>'+
									'</li>';	
								$('.usermesg').append(li);
							}
							break;
						case 'order':	// 我的订单
							order = className;
							break;
						case 'lineorder':	// 定制游
							lineorder = className;
							break;
						case 'coupon':	// 我的优惠券
							coupon = className;
							break;
						case 'wish':	// 我的收藏
							wish = className;						
							break;
						case 'mesg':	// 系统消息
							mesg = className;
							break;
						case 'profile':	// 编辑我的资料
							profile = className;
							$('.checkpic1 img').attr("src",icon);
							$('.listone img').attr("src",icon);				
							$('#nickname').val(nick);
							$('#name').val(realName);
							$('#tel').val(mobile);
							$('#email').val(email);
							$('#city').val(address);
							if(sex=='boy'){
								document.getElementById('boy').checked=true
							}else{
								document.getElementById('girl').checked=true
							}
							break;
						default:
							
					}
					
					if (account != null) {
						$('.login-register').show();
						$('.navLogin').hide();
						var len = $.fn.getByteLen(name);
						var px = len * 11;
						if(px < 80){
							px = 80;
						}
						var span =
							'<div style="width: '+px+'px;">'+
								'<div class="navHasLogin">'+
			                        '<a href="/uc/index.html">'+
										'<p><img src="'+icon+'">'+
											'<span>'+name+'<em></em></span>'+
										'</p>'+
									'</a>'+
			                        '<ul>'+
			                            '<li id="userindex" class="'+index+'"><a href="/uc/index.html">账号信息</a></li>'+
			                            '<li id="orders" class="'+order+'"><a href="/uc/order.html">我的订单</a></li>'+
			                            '<li id="orders" class="'+lineorder+'"><a href="/uc/lineorder.html">定制游</a></li>'+
			                            '<li id="coupons" class="'+coupon+'"><a href="/uc/coupon.html">我的优惠券</a></li>'+
			                            '<li id="wish" class="'+wish+'"><a href="/uc/wish.html">我的收藏</a></li>'+
			                            '<li id="post" class="'+mesg+'"><a href="/uc/mesg.html">系统消息</a></li>'+
			                            '<li id="profile" class="'+profile+'"><a href="/uc/profile.html">编辑我的资料</a></li>'+
			                            '<li><a onclick="top.location=\'/index.html\';$.cookie(\'nbminsu-uid\',null,{path:\'/\',expires:-1});return false;" href="javascript:window.opener=null;%20window.close();">退出</a></li>'+
			                        '</ul>'+
			                   '</div>'+
		                   '</div>';
						$('.login-register').append(span);
					}else{ 
						$('.login-register').hide();
						$('.navLogin').show();
						var span = 
/*								'<a href="/user/regist.html" class="name">注册</a>'+
								'<i></i>'+*/
								'<a href="/user/login.html">登录</a>';
						$('.navLogin').append(span);
					}
				},
			});
			
		}
	});
	
	/**
	 * -----------------------------------------
	 * jQuery类添加"成员函数"
	 * @author jjl<jiangjialiang@teemax.com.cn>
	 * @remark 
	 * -----------------------------------------
	 */
	$.fn.extend({
		/**
		 * 长度超出规定长度，显示省略号,
		 * 设置class为displayPart,
		 * 设置自定义属，displayLength可显示长度(不包含...),
		 * 双字节字符,长度*2.
		 * @param {String} content 内容
		 * @param {int} len 显示长度
		 * @return {String} result 返回新内容
		 */
        displayPart: function(content, len){
        	var displayLength = len;
//    		displayLength = this.attr("displayLength") || displayLength;
            var text = content;
            if (!text) return "";
            var result = "";
            var count = 0;
            for(var i=0; i<displayLength; i++){
                var _char = text.charAt(i);
                if (count >= displayLength) break;
                if (/[^x00-xff]/.test(_char)) count++;	// 双字节字符，[u4e00-u9fa5]中文  
                result += _char;
                count++;  
            }  
            if(result.length < text.length){
                result += "...";
            }
            return result; 
        },
        /**
         * 获取字符串长度(汉字算两个字符，字母数字算一个)
         * @param {String} val 值
         * @return {Int} len 长度
         */
        getByteLen: function(val) {
			var len = 0;
			for (var i = 0; i < val.length; i++) {
				var a = val.charAt(i);
				if (a.match(/[^\x00-\xff]/ig) != null) {
					len += 2;
				} else {
					len += 1;
				}
			}
			return len;
        },
        /**
         * 获取对象、数组的长度、元素个数
         * @param {Object/Array/String} obj 要计算长度的元素，可以为object、array、string
         * @return {Int} objLen 长度
         */
        count: function(obj){
			var objType = typeof obj;
			if(objType == "string"){
				return obj.length;
			}else if(objType == "object"){
				var objLen = 0;
				for(var i in obj){
					objLen++;
				}
				return objLen;
			}
			return false;
        },
        /**
         * 通过js正则验证手机号码的有效性
         * 验证130-139,150-159,180-189号码段的手机号码
         * @param {String} mobile 手机号
         * @return {boolean} true/false
         */
        validateMobile: function(mobile){
        	var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        	if(!myreg.test(mobile)){
        		swal('请输入有效的手机号码！');
        		return false;
        	}
        	return true;
        }
    });
  
	$.extend(MinSu, {
		MPage: MPage,
		MSearch: MSearch,
		MUtil: MUtil,
		MUser: MUser
	});
	
	$.extend(window, {
		MinSu: MinSu
	});

})(window, jQuery);